<?php
// 本类由系统自动生成，仅供测试用途
class IimageAction extends Action {
    public function add(){
    	$this->getMuen();
		
		$mid = $this->_get('mid');
		$type = $this->_get('type');
		$c = $this->_get('c');
		
		if(empty($mid) || empty($type) || empty($c)){
			$this->error('页面不存在');
		}elseif($type != 'miao' && $type != 'wang' && $type != 'book'){
			$this->error('页面不存在');
		}
		
		$goods = M('goods');
		$data = $goods->field('id,title')->where("`type`='".$type."'")->select();
		
		$this->assign('data', $data);
		$this->assign('mid', $mid);
		$this->assign('type', $type);
		$this->assign('c', $c);
		
		
    	$this->display();
    }
    
    public function addAction(){
    	$this->getMuen();
    	
    	$mid = $this->_get('mid');
		$type = $this->_get('type');
		$c = $this->_get('c');
    	$gid = $this->_post('gid');
    	
	    //上传图片
	    import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Uploads/';// 设置附件上传目录
		
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		
		$Img = M('index_module_img');
		
		$data = array(
			'gid' => $gid,
			'mid' => $mid,
			'img' => $info[0]['savename'],
	    	'ctime' => time(),
		);
		
		if($Img->add($data)){
		    $this->success('添加图片成功', '__APP__/'.$c.'/'.$type);
	    }else{
		    $this->error('添加图片失败');
	    }
		
    }
    
    public function index(){
	    $this->getMuen();
    	$mid = $this->_get('mid');
    	$type = $this->_get('type');
    	$c = $this->_get('c');
    	
    	$this->assign("type", $type);
    	
    	$Img = M('index_module_img');
    	$data = $Img->where('mid ='.$mid)->select();
    	
    	$this->assign("data", $data);
    	$this->display();
    	
    }
    
    public function delete(){
    	$this->getMuen();
    	
    	$id = $this->_get('id');
    	
    	$Img = M('index_module_img');
    	
	    if($Img->where('id='.$id)->delete()){
			$this->success('删除图片成功');
		}
    }
    
    public function edit(){
		$this->getMuen();
		$id = $this->_get('id');
		$type = $this->_get('type');
		$c = $this->_get('c');
				  
		$this->assign('type', $type);
		$this->assign('c', $c);
	  
		$goods = M('goods');
		$good = $goods->field('id,title')->where("`type`='".$type."'")->select();
		$this->assign("good", $good);
		
		$Img = M('index_module_img');
		$data = $Img->where('id = '.$id)->find();
		$this->assign("data", $data);
		
		$this->display();
    }
    
    public function editAction(){
    	$this->getMuen();
    	
    	$gid = $this->_post('gid');
    	$id = $this->_get('id');
    	
    	$type = $this->_get('type');
		$c = $this->_get('c');
		$mid = $this->_get('mid');
		
    	
    	//上传图片
	    import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Uploads/';// 设置附件上传目录
		
		$Img = M('index_module_img');
		
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$data = array(
				'img' => $info[0]['savename'],
				'gid' => $gid,
	    	);
	    	
			if($Img->where('id = '.$id)->save($data)){
				$this->success('修改图片成功', '__URL__/index/mid/'.$mid.'/type/'.$type.'/c/'.$c);
			}
		}	
    }
    
    public function getMuen(){
    	R("Login/isLogin");
    	
	   	$menu = include(COMMON_PATH.'menu.php');
		$this->assign('menu', $menu);
    }
}